import React, {Component} from 'react';
import {connect} from 'react-redux';

import reg from "../../utils/regexp";
import * as actionCreators from '../../actions/MemberAction';
import view from './view';

const loginParams = {
  m_name: '',
  m_pwd: '',
  gee_test: '1'
};

class Login extends Component {

  static navigationOptions = {
    header: null,
  };

  render() {
    return view(this, loginParams);
  }

  componentDidMount() {
    this.initialFormState()
  }

  initialFormState() {
    loginParams.m_name = '';
    loginParams.m_pwd = '';
    this.props.loginButtonState()
  }

}

const mapStateToProps = (state) => {
  return {
    loginButtonDisabled: state.member.loginButtonDisabled,
  }
};

const mapDispatchToProps = (dispatch) => {
  return {
    memberLogin(data) {
      dispatch(actionCreators.memberLogin(data))
    },
    loginButtonState() {
      let isLoginParamsEmpty = reg.regEmpty.test(loginParams.m_name) || reg.regEmpty.test(loginParams.m_pwd);
      if (isLoginParamsEmpty) {
        dispatch(actionCreators.loginButtonState(true))
      } else {
        dispatch(actionCreators.loginButtonState(false))
      }
    },
  }
};

export default connect(mapStateToProps, mapDispatchToProps)(Login)